package com.example.basecore.command;

import com.example.basecore.dto.Command;

import java.util.Objects;

/**
 * @author ycd0075
 * @date 2024-01-25 22:10
 */
public class DefaultCommandBus implements CommandBus{
    private CommandInvocation commandInvocation;

    public DefaultCommandBus(CommandInvocation commandInvocation) {
        this.commandInvocation = commandInvocation;
    }

    @Override
    public <R> R dispatch(Command c) {
        if(Objects.isNull(commandInvocation)){
            throw new IllegalArgumentException("CommandInvocation for [" + c.getClass().getName() +"] cannot found.");
        }
        return (R)commandInvocation.invoke(c);
    }
}
